package com.goldze.mvvmhabit.ui.food;

import android.app.Application;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.goldze.mvvmhabit.bean.LoginBean;
import com.goldze.mvvmhabit.data.DemoRepository;
import com.goldze.mvvmhabit.okgo.HTTPAPI;
import com.goldze.mvvmhabit.ui.register.RegisterActivity;
import com.goldze.mvvmhabit.ui.tab_bar.activity.TabBarActivity;
import com.goldze.mvvmhabit.utils.GsonUtil;
import com.goldze.mvvmhabit.utils.SpUtils;
import com.lzy.okgo.callback.StringCallback;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.LogUtil;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.Call;
import okhttp3.Response;

public class FoodViewModel extends BaseViewModel<DemoRepository> {
    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");
    //用户名清除按钮的显示隐藏绑定
    public ObservableInt clearBtnVisibility = new ObservableInt();
    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
    }

    public FoodViewModel(@NonNull Application application, DemoRepository repository) {
        super(application, repository);
        //从本地取得数据绑定到View层
        userName.set("33333");
        password.set("33333");
    }

    //注册
    public BindingCommand registerOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            SpUtils.put("module_register","register");
            startActivity(RegisterActivity.class);
        }
    });

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand clearUserNameOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userName.set("");
        }
    });
    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.pSwitchEvent.setValue(uc.pSwitchEvent.getValue() == null || !uc.pSwitchEvent.getValue());
        }
    });
    //用户名输入框焦点改变的回调事件
    public BindingCommand<Boolean> onFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus) {
                clearBtnVisibility.set(View.VISIBLE);
            } else {
                clearBtnVisibility.set(View.INVISIBLE);
            }
        }
    });
    //登录按钮的点击事件
    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            login();
        }
    });

    /**
     * 网络模拟一个登陆操作
     **/
    private void login() {
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }

        HTTPAPI.getInstance().login(userName.get(), password.get(), new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("注册-->" + s);

                LoginBean loginBean = GsonUtil.INSTANCE.parse(s, LoginBean.class);

                SpUtils.put("module_token",loginBean.getResult().getToken());
                SpUtils.put("module_person_id",loginBean.getResult().getUser().getId());
                SpUtils.put("module_username", loginBean.getResult().getUser().getUsername());
                SpUtils.put("module_name", loginBean.getResult().getUser().getName());
                SpUtils.put("module_tel", loginBean.getResult().getUser().getMobile());
                SpUtils.put("module_schoolname", loginBean.getResult().getUser().getSchool());
                SpUtils.put("module_age", loginBean.getResult().getUser().getAge());
                SpUtils.put("module_student_number",loginBean.getResult().getUser().getNumber());
                SpUtils.put("module_class_id", loginBean.getResult().getUser().getClazzId());
                SpUtils.put("module_class_name", loginBean.getResult().getUser().getClazzName());
                if (loginBean.getResult().getUser().getIdCard() != null) {
                    if (!TextUtils.isEmpty(loginBean.getResult().getUser().getId())) {
                        SpUtils.put("module_body_card_num", loginBean.getResult().getUser().getIdCard());
                    }
                }

                startActivity(TabBarActivity.class);
                finish();
            }
        });

    }



    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
